import os

import pandas as pd
import xlrd


"""读取Excel表数据"""
path = "/Users/lx/Desktop/xlsx"
files = os.listdir(path)
# print(files)
file_files = []
for file in files:
    if file.endswith('.xlsx'):
        # df = pd.read_excel(os.path.join(path, file))
        # 获取文件的前缀名称
        file_qian = file[0:len(file) - 5]
        file_files.append(file_qian)
# 定义一个空列表
result = []


def read_excel():
    # 获取第一行数据key
    first_row = sheet.row_values(0)

    # 从第一行用例开始循环
    for i in range(1, rows):
        # 定义空列表
        info_dict = {}
        # 再次循环取出列
        for j in range(0, cols):
            # 添加到字典中
            info_dict[first_row[j]] = sheet.row_values(i)[j]
            # print(info_dict)
        # 将数据放入列表中
        result.append(info_dict)
    # print(result)

    # c = open("../csv_util/csv_file.csv", "w")
    # for i in self.result:
    #     c.write(",".join(i) + "\n")
    # c.close()


# 传入参数，控制业务流程所需要的xlsx文件路径
def excel_read(ceshi):
    global sheet, rows, cols
    for i in range(len(file_files)):
        if file_files[i] == ceshi:
            # global rows, cols
            # 打开Excel文件
            excel = xlrd.open_workbook(path + "/" + file_files[i] + ".xlsx")

            # 通过下标定位表格
            sheet = excel.sheet_by_index(0)

            # 获取行和列数
            rows, cols = sheet.nrows, sheet.ncols
            read_excel()
            break

        # if file_files[i] == ceshi:
        #     # global rows, cols
        #     # 打开Excel文件
        #     excel = xlrd.open_workbook(path + "/" + file_files[i] + ".xlsx")
        #
        #     # 通过下标定位表格
        #     sheet = excel.sheet_by_index(0)
        #
        #     # 获取行和列数
        #     rows, cols = sheet.nrows, sheet.ncols
        #     read_excel()
        #     break
